import { getUserInfo } from '@/api/wx.js'
import { login } from "@/api/login";
/**
 * 微信授权,用户无感知
 */
export const wxAuth = async(redirectURL) => {
    // 获取微信授信配置
    try {
        // const context = await api.wechatLoginController.getWechatLoginSetDetail('MOBILE');
        // 获取公众号配置的appId
        // const appId = await getUserInfo();
        // const appId = 'wxd1835bceac6dc41e'
        // 这种方式用户无感知
        const scope = 'snsapi_base';
        //仅表示有无授权
        const state = 'isWxAuth';
        console.log('**********微信授权,用户无感知::state' + state + '::url:' + redirectURL);
        // redirectURL = encodeURIComponent(redirectURL);
        //授权地址
        // debugger
        // const authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectURL}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
        const authURL = redirectURL
        window.location.href = authURL;
    } catch (e) {
        // 提示不展示出来，输出到控制台即可,便于开发排查
        console.error('获取微信授权配置异常');
    }
};
/**
 * 授权逻辑
 * @param {*} searchObj 
 * @param {*} redirectUrl 
 */
export const handleWxAuth = async(searchObj, redirectUrl) => {
    let isAuth = localStorage.getItem("isAuth");

    if (searchObj && searchObj.code && !isAuth) {
        let res = await getUserInfo({ code: searchObj.code });
        localStorage.setItem("isAuth", 1);
        isAuth = 1;
        localStorage.setItem("userInfo", JSON.stringify(res.userInfo));
        localStorage.setItem("token", res.__token);
    }
    if (!isAuth) {
        wxAuth(redirectUrl);
    }
}